#include "mypiechart.h"
#include "ui_mypiechart.h"
#include <QPainter>
#include <QPaintEvent>
#include <math.h>
#include <QPen>
#include <QFont>


CMyPieChart::CMyPieChart(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::CMyPieChart)
{
    ui->setupUi(this);
    // 初始化饼图的比例数据
    m_ValueList = {3,5,10,24,26,32};
    // 饼图个比例的颜色
    m_ColorList = {"#3399FE","#77438B","#FFBF53","#F07474","#00AF50","#02B3C5"};
    m_nLineLength = 50;

}

CMyPieChart::~CMyPieChart()
{
    delete ui;
}

void CMyPieChart::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    // 设置抗锯齿
    painter.setRenderHint(QPainter::Antialiasing);
    // 设置文本抗锯齿
    painter.setRenderHint(QPainter::TextAntialiasing);
    // 将原始数据值转化为比例值
    QList<qreal> radioList = values2RadioList(m_ValueList);
    // 角度起始值和绘制角度范围
    QList<QPair<qreal,qreal>> OutList;
    // 第一个起始点的偏移值
    qreal dbOffset = 0;

    for(int i = 0;i< radioList.count() ;i++)
    {
        qreal dbEach = radioList.at(i);
        if(i == 0)
        {
            // 存放第一个扇形值
            OutList.append(qMakePair(dbOffset,dbEach*360));
        }
        else
        {
            // 上一个起始角度 + 上一个绘制角度范围,即是下一个起始角度
            OutList.append(qMakePair(OutList.last().first + OutList.last().second,dbEach*360));
        }
    }

    int nRadius = qMin(width(),height()) * 0.7;

    for(int j=0;j < OutList.count();j++)
    {
        // 饼图背景色
        QColor color = m_ColorList.at(j % m_ColorList.count());
        // 起始角度
        qreal dbStartAngle = OutList.at(j).first;
        // 绘制角度的跨度
        qreal dbSpanAngle = OutList.at(j).second;

        painter.setBrush(color);
        painter.setPen(Qt::NoPen);
        painter.setFont(QFont("黑体",14));
        // 绘制饼图，90度为逆时针旋转，默认3点除为0°。
        //drawPie: startAngle和spanAngle必须指定为1/16度，即一个完整的圆等于5760（16*360）。角度的正值表示逆时针方向，负值表示顺时针方向。零度位于3点钟位置。
        painter.drawPie((width()-nRadius)/2,(height()-nRadius)/2,nRadius,nRadius,
                        (90 - dbStartAngle)*16,-dbSpanAngle*16);
        painter.save();
        painter.setPen(QPen(QColor(color),2));
        painter.translate(width()/2,height()/2);
        // 角度的弧度值
        qreal dbRad = (-90 + dbStartAngle + dbSpanAngle/2)*(M_PI/180);

        qreal x1 = cos(dbRad) * (nRadius/2 + 10);
        qreal y1 = sin(dbRad) * (nRadius/2 + 10);
        qreal x2 = cos(dbRad) * (nRadius/2 + 35);
        qreal y2 = sin(dbRad) * (nRadius/2 + 35);
        // 绘制斜线
        painter.drawLine(x1,y1,x2,y2);
        // 判断水平线的增长方向
        bool bRightDirection = ((dbStartAngle + dbSpanAngle/2) <= 180);

        if(bRightDirection)
        {
            painter.drawLine(x2,y2,x2 + m_nLineLength,y2);
        }
        else
        {
             painter.drawLine(x2,y2,x2 - m_nLineLength,y2);
        }

        int nTextRectWidth = m_nLineLength;

        int nTextRectHeight = painter.font().pointSize();

        QRect rect(x2 + (bRightDirection?0:-nTextRectWidth),
                   y2 - nTextRectHeight - painter.pen().width() - 1,
                   nTextRectWidth,
                   nTextRectHeight);
        // 绘制文本
        painter.drawText(rect,Qt::AlignCenter,QString("%1%").arg(radioList.at(j)*100));
        painter.restore();

    }
}

// 将原始数值转化为比例数值
QList<qreal> CMyPieChart::values2RadioList(const QList<qreal>& valuelist)
{
    QList<qreal> radioList;
    qreal dbTotal = 0;
    for(int value:valuelist)
        dbTotal += value;

    for(int value:valuelist)
    {
        radioList.append((value/dbTotal));
    }

    return radioList;
}









